home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / tcl / xf2.3-p / xf2 / xf2.3 / xftutorial / xftutorial.tcl < prev   
Encoding:
Text File  |  1993-11-20  |  10.3 KB  |  452 lines

  1. #!wish -f
  2. # Program: xftutorial
  3. # Description: main part
  4. #
  5. # $Header: xftutorial.tcl[2.4] Mon Mar  8 01:25:25 1993 garfield@garfield frozen $
  6.  
  7. # global variables
  8. # external
  9. global argc
  10. global argv
  11. global tkVersion
  12.  
  13. # tutorial
  14. set tutBlock 0
  15. set tutChapter intro
  16. set tutSection 0
  17. set tutInterpreter "main.tcl"
  18. set tutScriptPath "./script"
  19.  
  20. # test tk version
  21. if {$tkVersion < 2.3} {
  22.   puts stderr "\nTutorial error: wrong TK version: need at least 2.3\n"
  23.   catch "destroy ."
  24.   catch "exit 0"
  25. }
  26.  
  27. if {$argc != 2} {
  28.   puts stderr "Please call the tutorial not directly!"
  29.   puts stderr "The program that should use the tutorial must"
  30.   puts stderr "provide a way to activate the tutorial."
  31.   puts stderr "In XF activate (Help|Tutorial)."
  32.   catch "destroy ."
  33.   catch "exit 0"
  34. }
  35.  
  36. set tutScriptPath [lindex $argv 0]
  37. set tutInterpreter [lindex $argv 1]
  38.  
  39. # execute action list
  40. proc TUTDoIt {} {
  41.   global tutBlock
  42.   global tutChapter
  43.   global tutSection
  44.   global ${tutChapter}LastSectionDone
  45.  
  46.   if {$tutBlock == 0} {
  47.     if {[set ${tutChapter}LastSectionDone] < $tutSection} {
  48.       TUTEvalCommand 0
  49.       set ${tutChapter}LastSectionDone $tutSection
  50.     }
  51.   }
  52. }
  53.  
  54. # eval command
  55. proc TUTEvalCommand {tutNormal} {
  56.   global tutBlock
  57.   global tutChapter
  58.   global tutSection
  59.   global tutInterpreter
  60.  
  61.   if {"[info globals ${tutChapter}Command${tutSection}]" != ""} {
  62.     global ${tutChapter}Command${tutSection}
  63.  
  64.     TUTBlock $tutNormal
  65.     set sendCommand "send $tutInterpreter \{XFTUTEval \{[set ${tutChapter}Command${tutSection}]\} \{[winfo name .]\}\}"
  66.     if {[catch $sendCommand tutResult]} {
  67.       # ignore timout
  68.       if {"remote interpreter did not respond" != "$tutResult"} {
  69.         puts stderr "Tutorial error: $tutResult"
  70.       }
  71.     }
  72.   }
  73. }
  74.  
  75. # block tutorial
  76. proc TUTBlock {tutNormal} {
  77.   global tutBlock
  78.   global tutSetNormal
  79.   
  80.   set tutBlock 1
  81.   set tutSetNormal $tutNormal
  82.   .frame2.prev configure -state disabled
  83.   .frame2.doit configure -state disabled
  84.   .frame2.next configure -state disabled
  85. }
  86.  
  87. # unblock tutorial
  88. proc TUTUnblock {} {
  89.   global tutBlock
  90.   global tutSetNormal
  91.   
  92.   set tutBlock 0
  93.   .frame2.prev configure -state normal
  94.   .frame2.next configure -state normal
  95.   if {$tutSetNormal == 1} {
  96.     .frame2.doit configure -state normal
  97.   }
  98. }
  99.  
  100. # insert string into list
  101. proc TUTInsertStringIntoList {tutList tutString} {
  102.  
  103.   set tutLength [string length $tutString]
  104.   set tutPosition 0
  105.   set tutOldPosition 0
  106.   while {$tutPosition < $tutLength} {
  107.     # currently the only working version
  108.     while {$tutPosition < $tutLength} {
  109.       set tutCurrent [string index $tutString $tutPosition]
  110.       if {[string match $tutCurrent "\n"] &&
  111.           ![string match $tutCurrent "\*"]} {
  112.         break
  113.       }
  114.       incr tutPosition 1
  115.     }
  116.     $tutList insert end \
  117.       "[string range $tutString $tutOldPosition $tutPosition]"
  118.     incr tutPosition 1
  119.     set tutOldPosition $tutPosition
  120.   }
  121. }
  122.  
  123. # next page
  124. proc TUTNextPage {} {
  125.   global tutBlock
  126.   global tutChapter
  127.   global tutSection
  128.   global ${tutChapter}Last
  129.   global ${tutChapter}LastSectionDone
  130.  
  131.   if {$tutBlock == 0} {
  132.     if {$tutSection > [set ${tutChapter}LastSectionDone]} {
  133.       TUTEvalCommand 1
  134.       set ${tutChapter}LastSectionDone $tutSection
  135.     }
  136.     if {$tutSection < [set ${tutChapter}Last]} {
  137.       incr tutSection 1
  138.     } {
  139.       set tutTmpChapter $tutChapter
  140.       case $tutTmpChapter in {
  141.         {intro} {
  142.           set tutChapter design
  143.         }
  144.         {design} {
  145.           set tutChapter working
  146.         }
  147.         {working} {
  148.           set tutChapter expert
  149.         }
  150.         {expert} {
  151.           set tutChapter expert
  152.         }
  153.         {packing} {
  154.           set tutChapter intro
  155.         }
  156.         {placing} {
  157.           set tutChapter intro
  158.         }
  159.         {example} {
  160.           set tutChapter intro
  161.         }
  162.       }
  163.       if {[string compare $tutTmpChapter expert] == 0} {
  164.         global ${tutChapter}Last
  165.  
  166.         set tutSection [set ${tutChapter}Last]
  167.       } {
  168.         set tutSection 0
  169.       }
  170.     }
  171.     TUTShowText
  172.   }
  173. }
  174.  
  175. # previous page
  176. proc TUTPrevPage {} {
  177.   global tutBlock
  178.   global tutChapter
  179.   global tutSection
  180.  
  181.   if {$tutBlock == 0} {
  182.     if {$tutSection > 0} {
  183.       incr tutSection -1
  184.     } {
  185.       set tutTmpChapter $tutChapter
  186.       case $tutTmpChapter in {
  187.         {intro} {
  188.           set tutChapter intro
  189.         }
  190.         {design} {
  191.           set tutChapter intro
  192.         }
  193.         {working} {
  194.           set tutChapter design
  195.         }
  196.         {expert} {
  197.           set tutChapter working
  198.         }
  199.         {packing} {
  200.           set tutChapter intro
  201.         }
  202.         {placing} {
  203.           set tutChapter intro
  204.         }
  205.         {example} {
  206.           set tutChapter intro
  207.         }
  208.       }
  209.       if {[string compare $tutTmpChapter intro] == 0} {
  210.         set tutSection 0
  211.       } {
  212.         global ${tutChapter}Last
  213.  
  214.         set tutSection [set ${tutChapter}Last]
  215.       }
  216.     }
  217.     TUTShowText
  218.   }
  219. }
  220.  
  221. # print text
  222. proc TUTPrintText {} {
  223.  
  224.   set outFile [open /tmp/xf-tut.ms w]
  225.   foreach tutChapter "header intro design working expert packing placing example" {
  226.     global ${tutChapter}Last
  227.  
  228.     set tutCounter 0
  229.     set tutChapterName ""
  230.     while {$tutCounter <= [set ${tutChapter}Last]} {
  231.       if {"[info globals ${tutChapter}Name${tutCounter}]" != ""} {
  232.         global ${tutChapter}Name$tutCounter
  233.         if {"$tutChapterName" == ""} {
  234.           set tutChapterName [set ${tutChapter}Name$tutCounter]
  235.           puts $outFile ".NH"
  236.           puts $outFile [string trim [set ${tutChapter}Name$tutCounter]]
  237.           puts $outFile ".PP"
  238.         } {
  239.           if {"$tutChapterName" != "[set ${tutChapter}Name$tutCounter]"} {
  240.             set tutChapterName [set ${tutChapter}Name$tutCounter]
  241.             puts $outFile ".NH 2"
  242.             puts $outFile [string trim [set ${tutChapter}Name$tutCounter]]
  243.             puts $outFile ".PP"
  244.           }
  245.         }
  246.       }
  247.       if {"[info globals ${tutChapter}Text${tutCounter}]" != ""} {
  248.         global ${tutChapter}Text$tutCounter
  249.         puts $outFile [string trim [set ${tutChapter}Text$tutCounter]]
  250.         puts $outFile ""
  251.       }
  252.       incr tutCounter 1
  253.     }
  254.   }
  255.   puts $outFile ""
  256.   close $outFile
  257. }
  258.  
  259. # set new chapter
  260. proc TUTSetChapter {tutNewChapter} {
  261.   global tutChapter
  262.   global tutSection
  263.  
  264.   set tutChapter $tutNewChapter
  265.   set tutSection 0
  266.  
  267.   TUTShowText
  268. }
  269.  
  270. # display text
  271. proc TUTShowText {} {
  272.   global tutBlock
  273.   global tutChapter
  274.   global tutSection
  275.   global ${tutChapter}Name${tutSection}
  276.   global ${tutChapter}Command${tutSection}
  277.   global ${tutChapter}LastSectionDone
  278.  
  279.   .frame5.chapter configure \
  280.     -text [set ${tutChapter}Name${tutSection}]
  281.   .frame5.page configure \
  282.     -text " Page: [expr ${tutSection}+1] "
  283.   if {"[info globals ${tutChapter}Text${tutSection}]" != ""} {
  284.     global ${tutChapter}Text${tutSection}
  285.  
  286.     if {[.frame3.thelist size] > 0} {
  287.       .frame3.thelist delete 0 end
  288.     }
  289.     TUTInsertStringIntoList .frame3.thelist \
  290.       [string trim [set ${tutChapter}Text${tutSection}]]
  291.   }
  292.   if {"[info globals ${tutChapter}Command${tutSection}]" != ""} {
  293.     if {[set ${tutChapter}LastSectionDone] < $tutSection} {
  294.       if {$tutBlock == 0} {
  295.         .frame2.prev configure \
  296.           -state normal
  297.         .frame2.doit configure \
  298.           -state normal
  299.         .frame2.next configure \
  300.           -state normal
  301.       }
  302.     } {
  303.       .frame2.prev configure \
  304.         -state normal
  305.       .frame2.doit configure \
  306.         -state disabled
  307.       .frame2.next configure \
  308.         -state normal
  309.     }
  310.   } {
  311.     .frame2.prev configure \
  312.       -state normal
  313.     .frame2.doit configure \
  314.       -state disabled
  315.     .frame2.next configure \
  316.       -state normal
  317.   }
  318. }
  319.  
  320. # create tutorial toplevel
  321. wm geometry . 420x420
  322. wm minsize . 100 100
  323. wm maxsize . 1000 1000
  324.  
  325. frame .frame4 \
  326.   -borderwidth 2 \
  327.   -relief raised
  328.  
  329. menubutton .frame4.file \
  330.   -text "File" \
  331.   -underline 0 \
  332.   -menu ".frame4.file.m"
  333.  
  334. menu .frame4.file.m
  335.      .frame4.file.m add command \
  336.        -label {Print to /tmp/xf-tut.ms} \
  337.        -underline 0 \
  338.        -command {TUTPrintText}
  339.      .frame4.file.m add separator
  340.      .frame4.file.m add command \
  341.        -label {Quit} \
  342.        -underline 0 \
  343.        -command {catch "destroy ."; catch "exit 0"}
  344.  
  345. menubutton .frame4.chapters \
  346.   -text "Chapters" \
  347.   -underline 0 \
  348.   -menu ".frame4.chapters.m"
  349.  
  350. menu .frame4.chapters.m
  351.  
  352. frame .frame5 \
  353.   -borderwidth 0
  354.  
  355. label .frame5.chapter \
  356.   -relief raised \
  357.   -text ""
  358.  
  359. label .frame5.page \
  360.   -relief raised \
  361.   -text ""
  362.  
  363. frame .frame2 \
  364.   -borderwidth 0
  365.  
  366. button .frame2.prev \
  367.   -text {Previous page} \
  368.   -command {TUTPrevPage}
  369.  
  370. button .frame2.doit \
  371.   -text {Perform action} \
  372.   -command {TUTDoIt}
  373.  
  374. button .frame2.next \
  375.   -text {Next page} \
  376.   -command {TUTNextPage}
  377.  
  378. frame .frame3 \
  379.   -borderwidth 0
  380.  
  381. scrollbar .frame3.vscroll \
  382.   -relief raised \
  383.   -command ".frame3.thelist yview"
  384.  
  385. scrollbar .frame3.hscroll \
  386.   -orient horiz \
  387.   -relief raised \
  388.   -command ".frame3.thelist xview"
  389.  
  390. listbox .frame3.thelist \
  391.   -exportselection false \
  392.   -relief raised \
  393.   -xscrollcommand ".frame3.hscroll set" \
  394.   -yscrollcommand ".frame3.vscroll set"
  395.  
  396. if {$tkVersion >= 3.0} {
  397.   tk_menuBar .frame4 .frame4.file .frame4.chapters
  398. } {
  399.   tk_menu . .frame4.file .frame4.chapters
  400. }
  401.  
  402. # packing
  403. pack append .frame2 \
  404.             .frame2.prev {left fill expand} \
  405.             .frame2.doit {left fill expand} \
  406.             .frame2.next {left fill expand}
  407. pack append .frame3 \
  408.             .frame3.vscroll {left filly} \
  409.             .frame3.hscroll {bottom fillx} \
  410.             .frame3.thelist {left fill expand}
  411. pack append .frame4 \
  412.             .frame4.file {left} \
  413.             .frame4.chapters {left}
  414. pack append .frame5 \
  415.             .frame5.page {right} \
  416.             .frame5.chapter {left fillx expand}
  417. pack append . \
  418.             .frame4 {top fillx} \
  419.             .frame5 {top fillx} \
  420.             .frame2 {bottom fill} \
  421.             .frame3 {bottom fill expand}
  422.  
  423. # load tutorial scripts
  424. if {[file exists $tutScriptPath]} {
  425.   foreach counter [exec ls $tutScriptPath] {
  426.     if {"[file extension $counter]" == ".scrpt"} {
  427.       source $tutScriptPath/$counter
  428.     }
  429.   }
  430. }
  431.  
  432. foreach counter $chapterList {
  433.   if {"$counter" != ""} {
  434.     .frame4.chapters.m add command \
  435.       -label "[lindex $counter 0]" \
  436.       -underline 0 \
  437.       -command "TUTSetChapter [lindex $counter 2]"
  438.     if {"[lindex $counter 1]" != ""} {
  439.       .frame4.chapters.m entryconfig last \
  440.         -underline [lindex $counter 1]
  441.     }
  442.   } {
  443.     .frame4.chapters.m add separator
  444.   }
  445. }
  446.  
  447. send $tutInterpreter {proc XFTUTEval {cmd interp} {eval $cmd; send $interp TUTUnblock}}
  448.  
  449. TUTSetChapter intro
  450.  
  451. # eof
  452.